﻿
local moduleName = "Maintank"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Maintank",

	["Scale"] = true,
	["Maintank frame scale."] = true
}
if GetLocale() == "zhCN" then
	L[moduleName] = "MT特效"

	L["Scale"] = "缩放"
	L["Maintank frame scale."] = "将MT的框缩放"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "MT特效"

	L["Scale"] = "縮放"
	L["Maintank frame scale."] = "將MT的框縮放"
end

----------------------------------
--      Local Declaration      --
----------------------------------



----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		frequency = {
			type = "range",
			name = L["Scale"],
			desc = L["Maintank frame scale."],
			min = 1,
		        max = 2.0,
		        step = 0.1,
		        get = "GetSetScale",
		        set = "GetSetScale",
		}
	}
}

plugin.defaultDB = {
	profile = {
		scale = 1.2
	}
}

plugin.MT = {}

----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:RegisterEvent("oRA_MainTankUpdate" , "UpdateAll")
	self:RegisterEvent("oRA_UpdateConfigGUI", "UpdateAll") -- for oRA player targets
	self:RegisterEvent("RAID_ROSTER_UPDATE" , "UpdateAll") -- for the blizzard buildin maintank table

	if _G.CT_RAOptions_UpdateMTs then
		hooksecurefunc("CT_RAOptions_UpdateMTs", function() self:UpdateAll() end)
	end
end


function plugin:OnUpdate(frame)
	if InCombatLockdown() then return end

	frame:SetScale(1.0)
	self.MT = {}
	-- check oRA2
	if oRA then
		if oRA.maintanktable then
			for _,v in pairs(oRA.maintanktable) do
				self.MT[v] = v
			end
		end

		if oRA:HasModule("OptionalPT") then
			for _,v in pairs(oRA:GetModule("OptionalPT").db.profile.playertable) do
				self.MT[v] = v
			end
		end
	end

	-- check CT_RaidAssist
	if CT_RA_MainTanks then
		for _,v in pairs(CT_RA_MainTanks) do
			self.MT[v] = v
		end
	end


	-- check the blizzard buildin maintank table
	if GetPartyAssignment("MAINTANK", frame.unit) or GetPartyAssignment("MAINASSIST", frame.unit) then
		self.MT[UnitName(frame.unit)] = UnitName(frame.unit)
		--frame:SetScale(self.db.profile.scale)
	end

	self:CheckTable(frame, self.MT)
end

function plugin:CheckTable(frame, tanks)
	local name = UnitName(frame.unit)

	for _,v in pairs(tanks) do -- the key is the tank number
		if v == name then
			frame:SetScale(self.db.profile.scale)
			break
		end
	end
end



----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetScale(_, v)
	if type(v) == "nil" then return self.db.profile.scale end

	-- this check is to assure we don't update to often
	if self.db.profile.scale ~= v then
		self.db.profile.scale = v

		self:UpdateAll()
	end
end
